package com.ruoyi.project.system.controller;

import java.util.HashMap;
import java.util.Map;

import com.ruoyi.framework.aspectj.lang.annotation.Anonymous;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.aliyuncs.push.model.v20160801.PushResponse;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.project.system.domain.PushMessageRequest;
import com.ruoyi.project.system.service.IAliyunPushService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 阿里云推送服务
 * 
 * @author ruoyi
 */
@Api(tags = "阿里云推送服务")
@RestController
@RequestMapping("/system/push")
@Anonymous
public class AliyunPushController extends BaseController {

    @Autowired
    private IAliyunPushService aliyunPushService;

    /**
     * 推送消息到所有设备
     */
    @ApiOperation("推送消息到所有设备")
//    @PreAuthorize("@ss.hasPermi('system:push:all')")
    @PostMapping("/all")
    public AjaxResult pushToAll(@RequestBody PushMessageRequest messageRequest) {
        if (StringUtils.isEmpty(messageRequest.getTitle())) {
            return error("标题不能为空");
        }
        if (StringUtils.isEmpty(messageRequest.getContent())) {
            return error("内容不能为空");
        }

        try {
            Map<String, Object> extras = messageRequest.getExtras() != null ? messageRequest.getExtras()
                    : new HashMap<>();

            PushResponse response = aliyunPushService.pushMessageToAll(
                    messageRequest.getTitle(),
                    messageRequest.getContent(),
                    extras);

            return success(response);
        } catch (Exception e) {
            logger.error("推送消息失败：", e);
            return error(e.getMessage());
        }
    }

    /**
     * 推送消息到指定设备
     */
    @ApiOperation("推送消息到指定设备")
    @PreAuthorize("@ss.hasPermi('system:push:device')")
    @PostMapping("/device")
    public AjaxResult pushToDevice(@RequestBody PushMessageRequest messageRequest) {
        if (StringUtils.isEmpty(messageRequest.getTitle())) {
            return error("标题不能为空");
        }
        if (StringUtils.isEmpty(messageRequest.getContent())) {
            return error("内容不能为空");
        }
        if (StringUtils.isEmpty(messageRequest.getTarget())) {
            return error("目标设备ID不能为空");
        }

        try {
            Map<String, Object> extras = messageRequest.getExtras() != null ? messageRequest.getExtras()
                    : new HashMap<>();

            PushResponse response = aliyunPushService.pushMessageToDevice(
                    messageRequest.getTarget(),
                    messageRequest.getTitle(),
                    messageRequest.getContent(),
                    extras);

            return success(response);
        } catch (Exception e) {
            logger.error("推送消息失败：", e);
            return error(e.getMessage());
        }
    }

    /**
     * 推送消息到指定别名
     */
    @ApiOperation("推送消息到指定别名")
    @PreAuthorize("@ss.hasPermi('system:push:alias')")
    @PostMapping("/alias")
    public AjaxResult pushToAlias(@RequestBody PushMessageRequest messageRequest) {
        if (StringUtils.isEmpty(messageRequest.getTitle())) {
            return error("标题不能为空");
        }
        if (StringUtils.isEmpty(messageRequest.getContent())) {
            return error("内容不能为空");
        }
        if (StringUtils.isEmpty(messageRequest.getTarget())) {
            return error("目标别名不能为空");
        }

        try {
            Map<String, Object> extras = messageRequest.getExtras() != null ? messageRequest.getExtras()
                    : new HashMap<>();

            PushResponse response = aliyunPushService.pushMessageToAlias(
                    messageRequest.getTarget(),
                    messageRequest.getTitle(),
                    messageRequest.getContent(),
                    extras);

            return success(response);
        } catch (Exception e) {
            logger.error("推送消息失败：", e);
            return error(e.getMessage());
        }
    }

    /**
     * 推送消息到指定账号
     */
    @ApiOperation("推送消息到指定账号")
    @PreAuthorize("@ss.hasPermi('system:push:account')")
    @PostMapping("/account")
    public AjaxResult pushToAccount(@RequestBody PushMessageRequest messageRequest) {
        if (StringUtils.isEmpty(messageRequest.getTitle())) {
            return error("标题不能为空");
        }
        if (StringUtils.isEmpty(messageRequest.getContent())) {
            return error("内容不能为空");
        }
        if (StringUtils.isEmpty(messageRequest.getTarget())) {
            return error("目标账号不能为空");
        }

        try {
            Map<String, Object> extras = messageRequest.getExtras() != null ? messageRequest.getExtras()
                    : new HashMap<>();

            PushResponse response = aliyunPushService.pushMessageToAccount(
                    messageRequest.getTarget(),
                    messageRequest.getTitle(),
                    messageRequest.getContent(),
                    extras);

            return success(response);
        } catch (Exception e) {
            logger.error("推送消息失败：", e);
            return error(e.getMessage());
        }
    }

    /**
     * 推送消息到指定标签
     */
    @ApiOperation("推送消息到指定标签")
    @PreAuthorize("@ss.hasPermi('system:push:tag')")
    @PostMapping("/tag")
    public AjaxResult pushToTag(@RequestBody PushMessageRequest messageRequest) {
        if (StringUtils.isEmpty(messageRequest.getTitle())) {
            return error("标题不能为空");
        }
        if (StringUtils.isEmpty(messageRequest.getContent())) {
            return error("内容不能为空");
        }
        if (StringUtils.isEmpty(messageRequest.getTarget())) {
            return error("目标标签不能为空");
        }

        try {
            Map<String, Object> extras = messageRequest.getExtras() != null ? messageRequest.getExtras()
                    : new HashMap<>();

            PushResponse response = aliyunPushService.pushMessageToTag(
                    messageRequest.getTarget(),
                    messageRequest.getTitle(),
                    messageRequest.getContent(),
                    extras);

            return success(response);
        } catch (Exception e) {
            logger.error("推送消息失败：", e);
            return error(e.getMessage());
        }
    }
}